package com.rp.system.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rp.common.aspect.annotetion.AutoLog;
import com.rp.common.aspect.annotetion.ResponseResultBody;
import com.rp.common.config.redis.RedisUtil;
import com.rp.common.result.ResultDTO;
import com.rp.system.user.model.SysPermissionTree;
import com.rp.system.user.service.SysPermissionService;
import com.rp.common.utils.NullIs;
import com.rp.common.utils.Selectway;
import com.rp.system.user.entity.SysPermission;
import com.rp.system.user.service.impl.SysPermissionServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author vicente
 * @since 2020-11-24
 */
@RestController
@RequestMapping("/sys-permission")
@Api(tags = "权限管理")
@ResponseResultBody
public class SysPermissionController {

    @Autowired
    private SysPermissionService sysPermissionService;

    @Autowired
    private Selectway selectway;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/list")
    @ApiOperation(value = "列表")
    @AutoLog(value = "权限管理-列表")
    @RequiresPermissions("sysPermission:list")
    public ResultDTO<LinkedHashSet<SysPermission>> list(
            @RequestParam(required = false) @ApiParam(value = "部门父级id") String parentId
    ) throws IllegalAccessException {
        if(!NullIs.isEmpty(parentId)){
            LinkedHashSet<SysPermission> list = sysPermissionService.getPermissionByDepartParentId(parentId);
            return ResultDTO.success().data(sysPermissionService.getTreeList(new ArrayList<>(list),false));
        }
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort_no").orderByAsc("create_time");
        List<SysPermission> list = sysPermissionService.list(queryWrapper);
        return ResultDTO.success().data(sysPermissionService.getTreeList(list,false));
    }

    @PostMapping("/insert")
    @AutoLog(value = "权限管理-增加")
    @ApiOperation(value = "增加")
    @RequiresPermissions("sysPermission:update")
    public ResultDTO<IPage<SysPermission>> insert(@RequestBody SysPermission sysPermission) {
        if (NullIs.isEmpty(sysPermission.getParentId())) {
            sysPermission.setIsLeaf(0);
        }
        redisUtil.delAll(SysPermissionServiceImpl.KEY_STRING);
        return ResultDTO.tf(sysPermissionService.save(sysPermission));
    }

    @RequiresPermissions("sysPermission:update")
    @PutMapping("/update")
    @AutoLog(value = "权限管理-修改")
    @ApiOperation(value = "修改")
    public ResultDTO<IPage<SysPermission>> update(@RequestBody SysPermission sysPermission) {
        if (NullIs.isEmpty(sysPermission.getParentId())) {
            sysPermission.setIsLeaf(0);
        }
        redisUtil.delAll(SysPermissionServiceImpl.KEY_STRING);
        return ResultDTO.tf(sysPermissionService.updateById(sysPermission));
    }

    @RequiresPermissions("sysPermission:update")
    @DeleteMapping("/delete")
    @AutoLog(value = "权限管理-删除")
    @ApiOperation(value = "删除")
    public ResultDTO delete(@RequestBody @ApiParam(value = "id", required = true) String id) {
        redisUtil.delAll(SysPermissionServiceImpl.KEY_STRING);
        return ResultDTO.tf(sysPermissionService.removeById(id));
    }

    @RequiresPermissions("sysPermission:update")
    @DeleteMapping("/deleteAll")
    @AutoLog(value = "权限管理-批量删除")
    @ApiOperation(value = "批量删除")
    public ResultDTO deleteAll(@RequestBody @ApiParam(value = "id(英文逗号隔开)", required = true) String ids) {
        redisUtil.delAll(SysPermissionServiceImpl.KEY_STRING);
        return ResultDTO.tf(sysPermissionService.removeByIds(Arrays.asList(ids.split(","))));
    }

    @GetMapping("/findSysPermissionsByUserId")
//    @AutoLog(value = "权限管理-根据当前角色Id查询该角色所拥有的权限(侧边栏)")
    @ApiOperation(value = "根据当前角色Id查询该角色所拥有的权限(侧边栏)")
    public ResultDTO<LinkedHashSet<SysPermissionTree>> findSysPermissionsByUserId() throws IllegalAccessException {
        return ResultDTO.success().data(sysPermissionService.findSysPermissionsByUserIdInCache());
    }

    @GetMapping("/clearRedis")
    @AutoLog(value = "权限管理-清理缓存")
    @ApiOperation(value = "清理缓存")
    public ResultDTO clearRedis() {
        redisUtil.delAll(SysPermissionServiceImpl.KEY_STRING);
        return ResultDTO.success();
    }

}
